package gwtappcontainer.shared.apps.insight;

import gwtappcontainer.server.Utils;
import gwtappcontainer.server.apps.APIException;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apps.insight.Member.ContactDetails;
import gwtappcontainer.shared.apps.insight.ValidationResult.Status;

import java.util.List;

public class SyntaxChecker {		
	public static ValidationResult validate(Member.ContactDetails contactDetails) {
		if (null == contactDetails.email)
			return new ValidationResult(Status.ERROR_EMAIL_IS_NULL, "email is null");
		
		if (! Utils.isValidEmail(contactDetails.email))
			return new ValidationResult(Status.ERROR_EMAIL_INVALID, 
					"email [" + contactDetails + "] is invalid");				
				
		return new ValidationResult(Status.SUCCESS, "Success");	
	}
	
	public static void ensureValid(Member.ContactDetails contactDetails) {
		ValidationResult result = validate(contactDetails);
		
		if (result.status != Status.SUCCESS)
			throw new APIException(APIResponse.Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, 
					result.userFriendlyMessage);
	}
	
	static ValidationResult validate(List<Member.ContactDetails> contactDetailsList) {			
		for (ContactDetails contactDetail : contactDetailsList) {
			ValidationResult result = validate(contactDetail);
			if (result.status != Status.SUCCESS) {
				result.userFriendlyMessage = 
						"Invalid member [" + contactDetail.email + "] - " + result.userFriendlyMessage;
				return result;
			}															
		}
										
		return new ValidationResult(Status.SUCCESS, "Success");	
	}
	
	public static void ensureValid(List<Member.ContactDetails> contactDetailsList) {
		ValidationResult result = validate(contactDetailsList);
		
		if (result.status != Status.SUCCESS)
			throw new APIException(APIResponse.Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED, 
					result.userFriendlyMessage);
	}
}
